package com.blb.educourseservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.common.entity.CourseActivity;
import com.blb.educourseservice.mapper.CourseActivityMapper;
import com.blb.educourseservice.service.CourseActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourseActivityServiceImpl extends ServiceImpl<CourseActivityMapper, CourseActivity> implements CourseActivityService {

    @Autowired
    private CourseActivityMapper mapper;

    @Override
    public CourseActivity addStock(long activityId) {
        CourseActivity activity = mapper.selectById(activityId);
        if(activity != null){
            activity.setStock(activity.getStock() + 1);
            mapper.updateById(activity);
        }
        return activity;
    }

    @Override
    public CourseActivity reduceStock(long activityId) {
        CourseActivity activity = mapper.selectById(activityId);
        if(activity != null){
            if(activity.getStock() <= 0){
                throw new RuntimeException("库存不足!" + activity.getStock());
            }
            activity.setStock(activity.getStock() - 1);
            mapper.updateById(activity);
        }
        return activity;
    }
}
